package comgame.scenes;

import com.almasb.fxgl.audio.AudioPlayer;
import com.almasb.fxgl.audio.Sound;
import com.almasb.fxgl.dsl.FXGL;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 * 关卡详情UI类
 */
public class LevelDetailUI extends StackPane {
    private LevelData.LevelInfo levelInfo;
    private Runnable onChallenge;
    private Runnable onReturn;
    
    public LevelDetailUI(LevelData.LevelInfo levelInfo, Runnable onChallenge, Runnable onReturn) {
        this.levelInfo = levelInfo;
        this.onChallenge = onChallenge;
        this.onReturn = onReturn;
        initUI();
    }
    
    private void initUI() {
        // 设置UI样式 - 半透明黑色背景覆盖整个屏幕
        setStyle("-fx-background-color: rgba(0, 0, 0, 0.7);");
        setPrefSize(FXGL.getAppWidth(), FXGL.getAppHeight());
        
        // 创建主容器 - 居中显示，缩小尺寸
        VBox mainContainer = new VBox(20);
        mainContainer.setAlignment(Pos.CENTER);
        mainContainer.setPadding(new Insets(30));
        mainContainer.setPrefSize(550, 450);
        mainContainer.setStyle("-fx-background-color: linear-gradient(to bottom, #34495E, #2C3E50); " +
                            "-fx-background-radius: 20; " +
                            "-fx-border-color: #3498DB; " +
                            "-fx-border-width: 2; " +
                            "-fx-border-radius: 20; " +
                            "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 10, 0, 0, 5);");
        
        // 标题 - 添加装饰性边框
        VBox titleContainer = new VBox(5);
        titleContainer.setAlignment(Pos.CENTER);
        titleContainer.setPadding(new Insets(12, 25, 12, 25));
        titleContainer.setStyle("-fx-background-color: #E74C3C; -fx-background-radius: 10;");
        
        Label titleLabel = new Label("第 " + levelInfo.getLevel() + " 关");
        titleLabel.setStyle("-fx-font-size: 28px; -fx-font-weight: bold; -fx-text-fill: white;");
        
        Label subtitleLabel = new Label("准备挑战");
        subtitleLabel.setStyle("-fx-font-size: 14px; -fx-text-fill: #F8F9FA;");
        
        titleContainer.getChildren().addAll(titleLabel, subtitleLabel);
        
        // 怪物信息
        VBox monsterInfo = createMonsterInfo();
        
        // 掉落物信息
        VBox dropInfo = createDropInfo();
        
        // 按钮容器
        HBox buttonContainer = createButtons();
        
        mainContainer.getChildren().addAll(titleContainer, monsterInfo, dropInfo, buttonContainer);
        
        // 将主容器居中显示
        StackPane.setAlignment(mainContainer, Pos.CENTER);
        getChildren().add(mainContainer);
    }
    
    private VBox createMonsterInfo() {
        VBox container = new VBox(12);
        container.setAlignment(Pos.CENTER);
        container.setPadding(new Insets(15, 20, 15, 20));
        container.setStyle("-fx-background-color: rgba(52, 152, 219, 0.1); -fx-background-radius: 12; " +
                         "-fx-border-color: #3498DB; -fx-border-width: 1; -fx-border-radius: 12;");
        
        Label monsterTitle = new Label("⚔ 怪物信息");
        monsterTitle.setStyle("-fx-font-size: 20px; -fx-font-weight: bold; -fx-text-fill: #3498DB;");
        
        VBox statsContainer = new VBox(6);
        statsContainer.setAlignment(Pos.CENTER_LEFT);
        
        Label monsterName = new Label("🎯 怪物: " + levelInfo.getMonsterName());
        Label monsterHealth = new Label("❤ 血量: " + levelInfo.getMonsterHealth());
        Label monsterAttack = new Label("⚡ 攻击: " + levelInfo.getMonsterAttack());
        Label monsterDefense = new Label("🛡 防御: " + levelInfo.getMonsterDefense());
        Label monsterSpeed = new Label("⚡ 速度: " + levelInfo.getMonsterSpeed());
        Label monsterCrit = new Label("💥 暴击率: " + String.format("%.1f", levelInfo.getMonsterCritRate() * 100) + "%");
        
        String labelStyle = "-fx-font-size: 14px; -fx-text-fill: #ECF0F1; -fx-font-weight: 500;";
        monsterName.setStyle(labelStyle);
        monsterHealth.setStyle(labelStyle);
        monsterAttack.setStyle(labelStyle);
        monsterDefense.setStyle(labelStyle);
        monsterSpeed.setStyle(labelStyle);
        monsterCrit.setStyle(labelStyle);
        
        statsContainer.getChildren().addAll(monsterName, monsterHealth, monsterAttack, monsterDefense, monsterSpeed,monsterCrit);
        container.getChildren().addAll(monsterTitle, statsContainer);
        return container;
    }
    
    private VBox createDropInfo() {
        VBox container = new VBox(12);
        container.setAlignment(Pos.CENTER);
        container.setPadding(new Insets(15, 20, 15, 20));
        container.setStyle("-fx-background-color: rgba(231, 76, 60, 0.1); -fx-background-radius: 12; " +
                         "-fx-border-color: #E74C3C; -fx-border-width: 1; -fx-border-radius: 12;");
        
        Label dropTitle = new Label("💎 可能掉落");
        dropTitle.setStyle("-fx-font-size: 20px; -fx-font-weight: bold; -fx-text-fill: #E74C3C;");
        
        VBox itemsContainer = new VBox(6);
        itemsContainer.setAlignment(Pos.CENTER_LEFT);
        
        // 根据关卡显示详细的掉落物信息
        int level = levelInfo.getLevel();
        switch (level) {
            case 1:
                itemsContainer.getChildren().addAll(
                    createDropLabel("史莱姆凝胶 2-3个 (100%) - 单价2元"),
                    createDropLabel("随机一级作物种子 2个 (10%)")
                );
                break;
            case 2:
                itemsContainer.getChildren().addAll(
                    createDropLabel("昆虫肉 1-2个 (100%) - 单价5元"),
                    createDropLabel("霜瓜种子 1-2个 (20%)")
                );
                break;
            case 3:
                itemsContainer.getChildren().addAll(
                    createDropLabel("妙蛙种子 (可服役)"),
                    createDropLabel("捕捉失败 (50%)"),
                    createDropLabel("普通级 (30%) - 2秒一步自动"),
                    createDropLabel("优秀级 (15%) - 1秒自动"),
                    createDropLabel("史诗级 (4%) - 0.5秒一步"),
                    createDropLabel("传说级 (1%) - 0.1秒一步")
                );
                break;
            case 4:
                itemsContainer.getChildren().addAll(
                    createDropLabel("装备掉落："),
                    createDropLabel("  什么都没有 (30%)"),
                    createDropLabel("  幽灵剑 (15%)"),
                    createDropLabel("  幽灵一套 (55%/4%)"),
                    createDropLabel("种子掉落："),
                    createDropLabel("  霜瓜 8-14个 (85%)"),
                    createDropLabel("  其他种子 3-6个 (60%)")
                );
                break;
            case 5:
                itemsContainer.getChildren().addAll(
                    createDropLabel("随机一级种子 15-24个 (40%)"),
                    createDropLabel("随机二级种子 8-14个 (30%)"),
                    createDropLabel("齐瓜 5-10个 (30%)"),
                    createDropLabel("额外同类型种子 4-8个 (80%)")
                );
                break;
            case 6:
                itemsContainer.getChildren().addAll(
                    createDropLabel("南瓜王 (50%)："),
                    createDropLabel("  无头骑士剑 (37%)"),
                    createDropLabel("  南瓜种子 100个 (100%)"),
                    createDropLabel("常绿尖叫怪 (50%)："),
                    createDropLabel("  喜庆之翼 (37%)")
                );
                break;
            case 7:
                itemsContainer.getChildren().addAll(
                    createDropLabel("空间之主一套 (30%)"),
                    createDropLabel("泰拉棱镜 (30%)")
                );
                break;
            default:
                // 默认显示原来的两个掉落物
                Label dropItem1 = new Label("✨ " + levelInfo.getDropItem1());
                Label dropItem2 = new Label("✨ " + levelInfo.getDropItem2());
                String labelStyle = "-fx-font-size: 14px; -fx-text-fill: #F39C12; -fx-font-weight: 500;";
                dropItem1.setStyle(labelStyle);
                dropItem2.setStyle(labelStyle);
                itemsContainer.getChildren().addAll(dropItem1, dropItem2);
                break;
        }
        
        container.getChildren().addAll(dropTitle, itemsContainer);
        return container;
    }
    
    private Label createDropLabel(String text) {
        Label label = new Label(text);
        label.setStyle("-fx-font-size: 12px; -fx-text-fill: #F39C12; -fx-font-weight: 500;");
        return label;
    }
    
    private HBox createButtons() {
        HBox container = new HBox(20);
        container.setAlignment(Pos.CENTER);
        container.setPadding(new Insets(15, 0, 0, 0));
        
        // 加载音效
        AudioPlayer audioPlayer = FXGL.getAudioPlayer();
        Sound clickSound = FXGL.getAssetLoader().loadSound("drop.wav");
        
        // 挑战按钮
        Button challengeBtn = new Button("⚔ 开始挑战");
        challengeBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #E74C3C, #C0392B); " +
                           "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                           "-fx-padding: 10 30; -fx-background-radius: 10; " +
                           "-fx-border-color: #C0392B; -fx-border-width: 2; -fx-border-radius: 10; " +
                           "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
        
        // 添加悬停效果
        challengeBtn.setOnMouseEntered(e -> {
            challengeBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #C0392B, #A93226); " +
                               "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                               "-fx-padding: 10 30; -fx-background-radius: 10; " +
                               "-fx-border-color: #A93226; -fx-border-width: 2; -fx-border-radius: 10; " +
                               "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 8, 0, 0, 3);");
        });
        
        challengeBtn.setOnMouseExited(e -> {
            challengeBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #E74C3C, #C0392B); " +
                               "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                               "-fx-padding: 10 30; -fx-background-radius: 10; " +
                               "-fx-border-color: #C0392B; -fx-border-width: 2; -fx-border-radius: 10; " +
                               "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
        });
        
        challengeBtn.setOnMouseClicked(e -> {
            audioPlayer.playSound(clickSound);
            if (onChallenge != null) {
                onChallenge.run();
            }
        });
        
        // 返回按钮
        Button returnBtn = new Button("🔙 返回");
        returnBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #95A5A6, #7F8C8D); " +
                         "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                         "-fx-padding: 10 30; -fx-background-radius: 10; " +
                         "-fx-border-color: #7F8C8D; -fx-border-width: 2; -fx-border-radius: 10; " +
                         "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
        
        // 添加悬停效果
        returnBtn.setOnMouseEntered(e -> {
            returnBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #7F8C8D, #6C7B7D); " +
                             "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                             "-fx-padding: 10 30; -fx-background-radius: 10; " +
                             "-fx-border-color: #6C7B7D; -fx-border-width: 2; -fx-border-radius: 10; " +
                             "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 8, 0, 0, 3);");
        });
        
        returnBtn.setOnMouseExited(e -> {
            returnBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #95A5A6, #7F8C8D); " +
                             "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                             "-fx-padding: 10 30; -fx-background-radius: 10; " +
                             "-fx-border-color: #7F8C8D; -fx-border-width: 2; -fx-border-radius: 10; " +
                             "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
        });
        
        returnBtn.setOnMouseClicked(e -> {
            audioPlayer.playSound(clickSound);
            if (onReturn != null) {
                onReturn.run();
            }
        });
        
        container.getChildren().addAll(challengeBtn, returnBtn);
        return container;
    }
} 